import { storage } from "../../../common/index";
Component({
  properties: {
    data: {
      type: Object,
      default: {},
    },
    currentGroup: {
      type: Object,
      default: {},
    },
  },
  data: {
    bookList: [],
    groupData: {},
    current: {},
  },
  observers: {
    "data.**": function (field) {
      const bookList = [];
      field.bookList.map((item) => {
        const book = storage.data.bookList.find((book) => book.id === item);
        if (book) {
          bookList.push(book);
        }
      });
      const groupData = {
        name: field.name,
        imgs: [],
        bookList: bookList,
        id: field.id,
      };
      bookList.map((item) => {
        if (groupData.imgs.length < 4) {
          groupData.imgs.push(item.image);
        }
      });
      this.setData({
        bookList: bookList,
        groupData: groupData,
      });
    },
  },
  lifetimes: {
    attached: function () {
      const bookList = [];
      this.data.data.bookList.map((item) => {
        const book = storage.data.bookList.find((book) => book.id === item);
        if (book) {
          bookList.push(book);
        }
      });
      const groupData = {
        name: this.data.data.name,
        imgs: [],
        bookList: bookList,
        id: this.data.data.id,
      };
      bookList.map((item) => {
        if (groupData.imgs.length < 4) {
          groupData.imgs.push(item.image);
        }
      });
      this.setData({
        bookList: bookList,
        groupData: groupData,
      });
    },
  },
});
